<?php
include("utils.php");
if($_SESSION['siteuserId']=="")
{
	header("location:start.php");
}
if($_POST['mode']=="change_status")
{
	change_status();
}
else
{
	main_content("main();");
}

function change_status()
{
  echo $sql = "UPDATE ".SITEMASTER." SET status = if(status = 'N','Y','N') WHERE adsiteId = '".$_POST['Id']."' ";
	mysql_query($sql);
	$GLOBALS['err_msg']="Status Changed Successfully";
	main_content("main();");

}

function main()
{

if($_SESSION['languageId']!="")
{
	$languageId=$_SESSION['languageId'];
}
else
{
	$languageId='1';
}

if($_REQUEST['mode']=='publisher_statistics')
{
	$today=date("Y-m-d");
	if($_REQUEST['ad_site']!='' && $_REQUEST['ad_type']=='' && $_REQUEST['ad_status']=='')
	{
		
		$sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  ADM.UserId = '".$_SESSION['siteuserId']."' and AM.startDate > '".$_REQUEST['start_date']."' AND AM.endDate < '".$_REQUEST['end_date']."' and SM.adSiteUrl = '".$_REQUEST['ad_site']."'";
	}
	else if($_REQUEST['ad_site']=='' && $_REQUEST['ad_status']=='' && $_REQUEST['ad_type']!='')
{
	$sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  ADM.UserId = '".$_SESSION['siteuserId']."' and AM.startDate > '".$_REQUEST['start_date']."' AND AM.endDate < '".$_REQUEST['end_date']."' and ASD.adspaceType = '".$_REQUEST['ad_type']."'";
}
else if($_REQUEST['ad_site']=='' && $_REQUEST['ad_type']=='' && $_REQUEST['ad_status']!='')
{
	if($_REQUEST['ad_status']=='ending')
	{
	$sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  ADM.UserId = '".$_SESSION['siteuserId']."' and  AM.endDate = '".$today."'";
	}
	else if($_REQUEST['ad_status']=='expired')
	{
	$sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  ADM.UserId = '".$_SESSION['siteuserId']."' and  AM.endDate < '".$today."'";
	}

}
else if($_REQUEST['ad_site']!='' && $_REQUEST['ad_type']!='' && $_REQUEST['ad_status']=='')
{
	$sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  ADM.UserId = '".$_SESSION['siteuserId']."' and AM.startDate > '".$_REQUEST['start_date']."' AND AM.endDate < '".$_REQUEST['end_date']."' and SM.adSiteUrl = '".$_REQUEST['ad_site']."' and ASD.adspaceType = '".$_REQUEST['ad_type']."'";
}
else if($_REQUEST['ad_site']=='' && $_REQUEST['ad_type']!='' && $_REQUEST['ad_status']!='')
{
	if($_REQUEST['ad_status']=='ending')
	{
	$sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  ADM.UserId = '".$_SESSION['siteuserId']."' AND AM.endDate = '".$today."' AND ASD.adspaceType = '".$_REQUEST['ad_type']."'";
	}
	else if($_REQUEST['ad_status']=='expired')
	{
	$sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  ADM.UserId = '".$_SESSION['siteuserId']."' AND AM.endDate < '".$today."' AND ASD.adspaceType = '".$_REQUEST['ad_type']."'";
	}
}
else if($_REQUEST['ad_site']!='' && $_REQUEST['ad_type']=='' && $_REQUEST['ad_status']!='')
{
	if($_REQUEST['ad_status']=='ending')
	{
	$sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  ADM.UserId = '".$_SESSION['siteuserId']."' AND AM.endDate = '".$today."' AND SM.adSiteUrl = '".$_REQUEST['ad_site']."'";
	}
	else if($_REQUEST['ad_status']=='expired')
	{
	$sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  ADM.UserId = '".$_SESSION['siteuserId']."' AND AM.endDate < '".$today."' AND SM.adSiteUrl = '".$_REQUEST['ad_site']."'";
	}
}
else if($_REQUEST['ad_site']!='' && $_REQUEST['ad_type']!='' && $_REQUEST['ad_status']!='')
{
	if($_REQUEST['ad_status']=='ending')
	{
	$sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  ADM.UserId = '".$_SESSION['siteuserId']."' AND AM.endDate = '".$today."' AND SM.adSiteUrl = '".$_REQUEST['ad_site']."' and ASD.adspaceType = '".$_REQUEST['ad_type']."'";
	}
	else if($_REQUEST['ad_status']=='expired')
	{
	$sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  ADM.UserId = '".$_SESSION['siteuserId']."' AND AM.endDate < '".$today."' AND SM.adSiteUrl = '".$_REQUEST['ad_site']."' and ASD.adspaceType = '".$_REQUEST['ad_type']."'";
	}
}
else
{
	$sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  ADM.UserId = '".$_SESSION['siteuserId']."' and AM.startDate > '".$_REQUEST['start_date']."' AND AM.endDate < '".$_REQUEST['end_date']."'";
}
// if($_REQUEST['purchase_type']!='')
// {
// 	if($_REQUEST['purchase_type']=='direct')
// 	{
// 		$sql_details.= "and AD.networkId = 0"; 
// 	}
// 	if($_REQUEST['purchase_type']=='network')
// 	{
// 		echo $sql_details.= "and AD.networkId > 0"; 
// 	}
// }
}





else
{

 $sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and ADM.UserId = '".$_SESSION['siteuserId']."'";

}
$query_details=mysql_query($sql_details);
$num=mysql_num_rows($query_details);

 $query_site_name=mysql_query("select adSiteName from adsite_master where siteuserId='".$_SESSION['siteuserId']."' and langId='".$languageId."'");

?><script language="JavaScript">
function show(argu)
{
	//alert(argu);
	document.adspaces.site_type.value=argu;
	document.adspaces.submit();
}
function show_site_nane(argu1)
{
	//alert(argu1);
	document.adspaces.site_name.value=argu1;
	document.adspaces.submit();
}
function change_status(argu3)
{
	//alert(argu3);

	document.adspaces.Id.value=argu3;
	document.adspaces.mode.value="change_status";
	document.adspaces.submit();
}
</script>

<script language="JavaScript">
function search_pub_statistics()
{
document.adspaces.mode.value="publisher_statistics";
document.adspaces.submit();
}
</script>
<!--<SCRIPT language=javascript src="calender/js/popcalendar.js"></SCRIPT>-->
<script language="javascript" type="text/javascript" src="JavaScripts/datetimepicker.js"></script>

<form name="adspaces" action="" method="POST" id="adspaces" onsubmit="return search_pub_statistics();">
<input type="hidden" name="site_type" value="">
<input type="hidden" name="mode" value="">
<input type="hidden" name="site_name" value="">
<input type="hidden" name="Id" value=""><br />
<br />
<br />


<table width="90%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td valign="top" align="center"><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
                  <tr>
                    <td valign="top"></td>
                    <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td width="11"><img src="images/PinkBorderTopLeft.jpg" alt="" width="11" height="11"></td>
                              <td class="PinkBoxTopBg"><img src="images/spacer.gif" alt="" width="10" height="11"></td>
                              <td width="11"><img src="images/PinkBorderTopRight.jpg" alt="" width="11" height="11"></td>
                            </tr>
                        </table></td>
                      </tr>
                      <tr>
                        <td class="PinkTwosideBorder" valign="top"><table width="100%" class="center" cellspacing="0" cellpadding="0">
 
  <tr>
    <td align="left" valign="top">

<!-- start table  -->

<table id="spacesList" style="width:100%;margin-left:1px;position:relative" align="center" border="0" >

<tr>
    <td colspan="2" align="center" class="sky font-18" style="padding-bottom:20px;">
	My Websites
	</td>
<?php
if($GLOBALS['err_msg']!="")
{
?>
<tr>
    <td colspan="2" align="center" class="font-11 latest_heading2" style="padding-bottom:10px;">
	<strong><?php echo $GLOBALS['err_msg'];?></strong>
	</td>
</tr>
<?php
}
?>
<?php
$current_date=date("Y-m-d");
//echo $current_date;
$explode_current_date=explode("-",$current_date);

$explode_next_year=$explode_current_date[0]+1;
$nextyear=$explode_next_year."-".date("m")."-".date("d");

$explode_previous_year=$explode_current_date[0]-1;
$previousyear=$explode_previous_year."-".date("m")."-".date("d");

$select_site="select * from ".SITEMASTER." order by adsiteId";
$exe_site=mysql_query($select_site);
?>
  <tr>
	<td style="text-align:left;white-space:nowrap;padding-bottom:10px;" colspan="" valign="middle">


      <span class="font-12 black advertise_tableTxt">
        <!--Show starts from:<input type="text" name="start_date" id="start_date" value="<?php// echo $previousyear; ?>" class="InputBox" onClick="popUpCalendar(this, document.adspaces.start_date,'yyyy-mm-dd');" style="width:100;" maxlength="100" >&nbsp;to:&nbsp; 
	<input type="text" name="end_date" id="end_date" value="<?php// echo $nextyear; ?>" class="InputBox" onClick="popUpCalendar(this, document.adspaces.end_date,'yyyy-mm-dd');" style="width:100" maxlength="100">-->

      Show starts from:<input type="text" name="start_date" id="start_date" value="<?php echo $previousyear; ?>" class="InputBox" onClick="javascript:NewCal('start_date','yyyymmdd',false,24);" style="width:100;" maxlength="100" >&nbsp;to:&nbsp; 
	<input type="text" name="end_date" id="end_date" value="<?php echo $nextyear; ?>" class="InputBox" onClick="javascript:NewCal('end_date','yyyymmdd',false,24);" style="width:100" maxlength="100">
        Site:
        <select id="ad_site" name="ad_site" class="InputBox" style="width: 100px;" maxlength="100">
          <option value="">All</option>
	<?php
	while($row_site=mysql_fetch_array($exe_site))
	{
	?>
          <option value="<?php echo $row_site['adSiteUrl']; ?>"><?php echo $row_site['adSiteUrl']; ?></option>
	<?php
	}
	?>
          </select>

      </span>

<span class="font-12 black advertise_tableTxt">
Ad Type:<select id="ad_type" name="ad_type" class="InputBox" style="width: 100px;" maxlength="100">
	  <option value="">All</option>
          <option value="text">Text</option>
	  <option value="banner">Banner</option>
	  <option value="thumbnail">Thumbnail</option>
	  <option value="commercial break">Commercial Break</option>
</select>
</span>

<span class="font-12 black advertise_tableTxt">
Status:<select id="ad_status" name="ad_status" class="InputBox" style="width: 100px;" maxlength="100">
	  <option value="">Current</option>
          <option value="ending">Ending</option>
	  <option value="expired">Expired</option>
</select>
</span>

		<span class="font-12 black advertise_tableTxt">
	<input type="submit" name="search_publisher_statistics" id="search_publisher_statistics" value="Go" class="blank-button-big-add" />
	</span>

    </td>




  </tr>


  <tr>

    <td colspan="2">

<table border="1" cellspacing="1" cellpadding="0" width="100%">
 <TR>
  <TD align="center">
   <table width="100%" class="login_txt" cellspacing="1" cellpadding="0" border="0">
    <tr style="background-color:gray">
        <th class="listHeader" align="center" nowrap="true">
        <strong>Run Dates</strong>
        </th>
        <th class="listHeader" align="center" nowrap="true">
        Days
        </th>
        <th class="listHeader" align="center" nowrap="true">
         Website
        </th>
        <th class="listHeader" align="center" nowrap="true">
         AD Space
        </th>
        <th class="listHeader" align="center" nowrap="true">
         Ad Type
        </th>
		<th class="listHeader" align="center" nowrap="true">
         Site Type
        </th>
		<th class="listHeader" align="center" nowrap="true">
         Impressions
       </th>

		<th class="listHeader" align="center" nowrap="true">
         Clicks
       </th>

		<th class="listHeader" align="center" nowrap="true">
         Avg CPC
       </th>

	    <?php
		if($num>0)
		{

		$loop=0;
		while($rec_details=mysql_fetch_array($query_details))
		{
		$loop++;
		if($loop%2 == 0) 
		{
		$class1="normalActive2";
		$class2="normal2";
		$class3="normal2";
		$class4="gray_sitename2";
		}
		else 
		{
		$class1="normalActive";
		$class2="normal";
		$class3="normal";
		$class4="white_sitename2";
		}

		 $sql_days="select * from advertise_master where advertiseId='".$rec_details['advertiseId']."' ";
			$rec_days=mysql_fetch_array(mysql_query($sql_days));

		 $sql_website="select * from adsite_master where adsiteId='".$rec_details['adsiteId']."' ";
			$rec_website=mysql_fetch_array(mysql_query($sql_website));

		 $sqlPrice="select * FROM ".PRICE." where adspaceId='".$rec_details['adspaceId']."' order by duration asc";
		$queryPrice=mysql_query($sqlPrice);
		$resultPrice= mysql_fetch_array($queryPrice);
		 $resultPrice[ammount];
	    ?>

        <tr onmouseover="this.className='<?php echo $class1;?>'" onmouseout="this.className='<?php echo $class2;?>'" class="<?php echo $class3;?>">

        <td class="<?php echo $class4;?>" align="left">

		<!--<a href="edit_websites.php?sid=<?php echo $rec_days['adsiteId'];?>" class="register_link"><?php echo $rec_days['startDate'];?> - <?php echo $rec_days['endDate'];?></a>-->
		<?php echo $rec_days['startDate'];?> - <?php echo $rec_days['endDate'];?>
		&nbsp;
		</td>
        <td class="<?php echo $class4;?>" align="left">
<?php
 $date1=date('m/d/Y',strtotime($rec_days['startDate']));
 $date2=date('m/d/Y',strtotime($rec_days['endDate']));

  echo $date=dateDiff("/", $date2, $date1);

//echo gregoriantojd(03,19,2009);
// echo $date=($rec_details['endDate']-$rec_details['startDate']);

?>&nbsp;</td>

        <td class="<?php echo $class4;?>" align="left">
		<?php
		echo $rec_website['adSiteUrl'];
		?>
		&nbsp;
		</td>
        <td class="<?php echo $class4;?>" align="left"><?php echo $rec_details['adSpaceName'];?>&nbsp;</td>
        <td class="<?php echo $class4;?>" align="left"><?php echo $rec_details['adDisplayType'];?>
                          &nbsp;
                        </td>
		<td class="<?php echo $class4;?>" align="left"><?php echo $rec_website['adSiteType'];?>
                          &nbsp;
                        </td>
      
		<td class="<?php echo $class4;?>" align="left">

		<?php
		if($rec_details[clickPerDay]>0)
		{
			if($rec_details[clickPerDay]>$GLOBALS['avgImpression'])
			{
				//echo $rec=($rec_details[clickPerDay]/$GLOBALS['avgImpression']);
				echo $rec = number_format($rec_details[clickPerDay]/$GLOBALS['avgImpression'], 2, '.', '');
			}
			else
			{
				echo "0";
			}
		}
		else
			{
				echo "0";
			}
		?>

		&nbsp;
		</td>
		<td class="<?php echo $class4;?>" align="left">
			<?php

			if($rec_details[clickPerDay]==0)
			{
				$clickPerDay = '*New';
				$avgCPC = '*New';
			}
			else
			{
				$clickPerDay = $rec_details[clickPerDay]*$resultPrice[duration];
				$avgCPC = '$'.number_format($resultPrice[ammount]/$rec_details[clickPerDay], 2, '.', '');
			}
echo $clickPerDay;
			?>&nbsp;
		</td>
		<td class="<?php echo $class4;?>" align="left"><?php echo $avgCPC;?>&nbsp;</td>
		<!--<td align="left" class="<?php echo $class4;?>">
		 
        <?
	  		$status=mysql_fetch_array(mysql_query("select status from ".SITEMASTER." where adsiteId='".$rec_details['adsiteId']."' "));

			if($status['status']=='P')
			{				
	 	?>
      			
				<font color="#CC0000">Pending</font>
      <? 	} 
			elseif($status['status']=='N')
			{				
	 	?>
      			<a href="javascript:change_status('<?php echo $rec_details['adsiteId'];?>')" title="Activate Deactivate Status" class="register_link"> 
				<font color="#CC0000">Block</font></a>
      <? 	} 
	  		else
			{ ?>
      				<a href="javascript:change_status('<?php echo $rec_details['adsiteId'];?>')" title="Activate Deactivate Status" class="register_link"> <font color=\"#66CC00\">Active</font></a>
      <? 
	  		} 
	  ?>
                          mm
                        </td>-->
        </tr>
	<?php
	}
	} // end if
	else
	{
	?>
	   <tr><TD colspan="10" align="center" class="font-12 black advertise_tableTxt">No records found</TD></tr>

	<?php
	}
	?>

       </table>

    </td>
  </tr>
</table>

   </td>
  </tr>
</table>

<!-- e table  -->

</td>
</tr>
</table></td>
                      </tr>
                      <tr>
                        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td width="11"><img src="images/PinkBorderBottomLeft.jpg" alt="" width="11" height="11"></td>
                              <td class="PinkBoxBottomBg"><img src="images/spacer.gif" alt="" width="10" height="11"></td>
                              <td width="11"><img src="images/PinkBorderBottomRight.jpg" alt="" width="11" height="11"></td>
                            </tr>
                        </table></td>
                      </tr>
                    </table></td>
                    <td>&nbsp;</td>
                    </tr>
                </table></td>
              </tr>
            </table>
</form>
            
<?php
        //include("footer.php");
}
		?> 